﻿using BCCommon;
using BCCommon.Enums;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormDatabaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepFormDatabaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormDatabaseConfig;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;

namespace BCService.OA.WorkflowTemplate.WorkflowTemplateStepFormDatabaseConfig
{
	public class WorkflowTemplateStepFormDatabaseConfigService : IWorkflowTemplateStepFormDatabaseConfigService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IWorkflowTemplateStepFormDatabaseConfigData workflowTemplateStepFormDatabaseConfigData;
		private readonly string key = string.Format("{0}", EntityNameConst.WorkflowTemplateStepFormDatabaseConfigEntity);

		public WorkflowTemplateStepFormDatabaseConfigService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IWorkflowTemplateStepFormDatabaseConfigData workflowTemplateStepFormDatabaseConfigData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.workflowTemplateStepFormDatabaseConfigData = workflowTemplateStepFormDatabaseConfigData;
		}

		#region 检查工作流模板步骤表单数据源配置是否存在
		public bool Exists(long workflowTemplateStepFormDatabaseConfigId)
		{
			return this.workflowTemplateStepFormDatabaseConfigData.Exists(workflowTemplateStepFormDatabaseConfigId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加工作流模板步骤表单数据源配置
		public WorkflowTemplateStepFormDatabaseConfigDto Add(WorkflowTemplateStepFormDatabaseConfigRequestDto requestDto,AdminDto adminDto)
		{
			var entity = requestDto.As<WorkflowTemplateStepFormDatabaseConfigEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;

			var result = this.workflowTemplateStepFormDatabaseConfigData.Add(entity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormDatabaseConfigDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 修改工作流模板步骤表单数据源配置
		public WorkflowTemplateStepFormDatabaseConfigDto Update(WorkflowTemplateStepFormDatabaseConfigPutDto putDto, AdminDto adminDto)
		{
			if (!this.Exists(putDto.WorkflowTemplateStepFormDatabaseConfigId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<WorkflowTemplateStepFormDatabaseConfigEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			var result = this.workflowTemplateStepFormDatabaseConfigData.Update(entity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormDatabaseConfigDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 删除工作流模板步骤表单数据源配置
		public bool Delete(long workflowTemplateStepFormDatabaseConfigId)
		{
			if (!this.Exists(workflowTemplateStepFormDatabaseConfigId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.workflowTemplateStepFormDatabaseConfigData.Delete(workflowTemplateStepFormDatabaseConfigId).GetAwaiter().GetResult();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 获取工作流模板步骤表单数据源配置
		public WorkflowTemplateStepFormDatabaseConfigDto Get(long workflowTemplateStepFormDatabaseConfigId)
		{
			string cacheKey = this.redisService.GetKey(key, workflowTemplateStepFormDatabaseConfigId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateStepFormDatabaseConfigData.GetEntity(workflowTemplateStepFormDatabaseConfigId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<WorkflowTemplateStepFormDatabaseConfigDto>();
		}
		#endregion

		#region 获取工作流模板步骤表单数据源配置列表
		public IEnumerable<WorkflowTemplateStepFormDatabaseConfigDto> GetList(string formConfigCode)
		{
			string cacheKey = this.redisService.GetKey(key, formConfigCode);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.workflowTemplateStepFormDatabaseConfigData.GetList(formConfigCode).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<WorkflowTemplateStepFormDatabaseConfigDto>>();
		}
		#endregion

		#region 获取工作流模板步骤表单数据源配置分页
		public PageableList<WorkflowTemplateStepFormDatabaseConfigDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateStepFormDatabaseConfigData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<WorkflowTemplateStepFormDatabaseConfigDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<WorkflowTemplateStepFormDatabaseConfigDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}